package fetcher

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

func Fetch(url string) ([]byte, error) {
	//// 发送get请求
	//resp, err := http.Get(url)
	//if err != nil {
	//	return nil, err
	//}
	//
	//defer resp.Body.Close()

	client := &http.Client{}

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		panic(err)
		return nil, err
	}
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36") //刚才找到的浏览器中User-agent

	cookie1 := "sid=1c4a7334-0e30-4ba5-b29b-9239e6f84a81; ec=au5L2iX0-1606729816815-cdf4008d7c1271781883952; notificationPreAuthorizeSwitch=235778; Hm_lvt_2c8ad67df9e787ad29dbd54ee608f5d2=1606729825,1606800203,1606894732; __channelId=901045%2C0; Hm_lpvt_2c8ad67df9e787ad29dbd54ee608f5d2=1606978372; _exid=JHexxHA6q2Sw6UfmKTs%2Fkb8t0fIfhDDhqNWXVs07PDBjaBLZ2WxMS5AwEYa7j3bheeuJeKydrb9DIDkp78dnKw%3D%3D; _efmdata=ecoT%2BfaAyDvuzRz0MCbufgdumTxYu2QYpwm58GbBXYP2zIphVg3OMQ7hVw48nZyVmFCxC5yXxogJgwdIlDfGVgxYdVxsz8INxJaMvjxxGYM%3D"
	req.Header.Add("cookie", cookie1)

	resp, err := client.Do(req)
	if err != nil {
		log.Fatalln(err)
	}

	defer resp.Body.Close()

	// 判断返回的状态码
	if resp.StatusCode != http.StatusOK {
		//fmt.Println("Error:status code", resp.StatusCode)
		return nil, fmt.Errorf("Wrong status code : %d", resp.StatusCode)
	}
	return ioutil.ReadAll(resp.Body)
}
